<?php
declare (strict_types = 1);

namespace app\admin\model\project;

use bbweb\traits\ModelTrait;
use bbweb\basic\BaseModel;
use think\facade\Db;

/**
 * @mixin \think\Model
 */
class Swimlanes extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'project_swimlanes';

    use ModelTrait;


    // 设置字段信息
    protected $schema = [
        'id'			=> 'int',
        'project_id'    => 'int',
        'name'		    => 'varchar',
        'description'	=> 'varchar',
        'position'		=> 'int',
        'update_at'     => 'int',
    ];

    /**
     * 统计项目各栏目数量
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function toolTask($id)
    {
        $columns = Db::name('project_columns')->where('project_id', $id)->field('title,position')->order('position ASC')->select();
        $list = [];
        foreach ($columns as $column) {
            $list[$column['position']] = [
                'title' => $column['title'],
                'total' => Db::name('project_tasks')->where('project_id', $id)->where('column_id', $column['position'])->where('is_delete',0)->count('id')
            ];
        }
        if( self::update(['progress_list' => $list], ['id' => $id]) ) return true;
        return false;
    }

    /**
     * 获取项目的泳道
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getSwimlanes($id)
    {
        $data = [];
        $task_count = ProjectTasks::where('project_id','=',$id)->where('swimlane_id','=',0)->count();

        $swimlanes = self::where('project_id', $id)->field('id,project_id,name,description,position')->order('position ASC')->select()->toArray();
        if($task_count > 0 || empty($swimlanes)) {
            $data[] = [
                'id'            => 0,//泳道ID
                'project_id'    => $id,
                'name'          => '默认泳道',
                'description'   => '',
                'position'      => 0,
            ];
        }
        foreach ($swimlanes as $swim) {
            $data[] = $swim;
        }
        return $data;
    }

    /**
     * @param $id
     * @param int $project_id
     * @return bool
     */
    public static function remove($id, $project_id = 0)
    {
        self::beginTrans();
        ProjectTasks::update(['swimlane_id'=>0], ['swimlane_id'=>$id]);
        if(!self::where('id', $id)->delete()) {
            self::rollbackTrans();
            return false;
        }
        if($project_id) self::updatePositions($project_id);
        self::commitTrans();
        return true;
    }

    /**
     * @param $project_id
     * @return bool
     */
    private static function updatePositions($project_id)
    {
        $position = 0;
        $swimlanes = self::where('project_id', $project_id)
            ->order('position ASC,id ASC')
            ->column('id','id');

        if (!$swimlanes) {
            return false;
        }

        foreach ($swimlanes as $swimlane_id) {
            self::update(['position' => ++$position], ['id'=>$swimlane_id]);
        }
        return true;
    }
}
